package CAPA_INTERFAZ_DE_USUARIO;

import CAPA_LOGICA_DE_NEGOCIO.LN_LOGICA_NEGOCIO;
import RECURSOS.FONDO_FORM;
import RECURSOS.LOG;
import RECURSOS.MARCA;
import RECURSOS.MODELO;
import RECURSOS.PRODUCTO;
import RECURSOS.TIPO_PRODUCTO;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class IU_BUSCAR_PRODUCTO extends javax.swing.JFrame {
    
    private LN_LOGICA_NEGOCIO obj_ln;
    private PRODUCTO obj_producto;
    private FONDO_FORM F;
    
    @Override    
    public Image getIconImage() {
        Image retValue = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("RECURSOS/SOVI.PNG"));
        return retValue;
    }  
    
    public IU_BUSCAR_PRODUCTO() {
        initComponents();
        this.setLocationRelativeTo(null);
        F =new FONDO_FORM(this.getWidth()+30,this.getHeight(),"fondo.png");
        this.add(F);
        this.pack();  
        obj_ln=new LN_LOGICA_NEGOCIO();        
        obj_producto = new PRODUCTO();
         obj_producto.setModelo(new MODELO());
         obj_producto.getModelo().setMarca(new MARCA());
         obj_producto.setTipo_prod(new TIPO_PRODUCTO());         
    }
    
    //GETTERS

    public JLabel getLbl_titulo() {
        return lbl_titulo;
    }
    
    public FONDO_FORM getF() {
        return F;
    }   
    
    public PRODUCTO getObj_producto() {
        return obj_producto;
    }
     
    public JComboBox getCbx_marca() {
        return cbx_marca;
    }

    public JComboBox getCbx_modelo() {
        return cbx_modelo;
    }

    public JComboBox getCbx_tipoproducto() {
        return cbx_tipoproducto;
    }

    public JButton getBtn_buscar() {
        return btn_buscar;
    }        
    
    public LN_LOGICA_NEGOCIO getObj_ln() {
        return obj_ln;
    }           
            
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbl_tipo_producto = new javax.swing.JLabel();
        lbl_titulo = new javax.swing.JLabel();
        cbx_marca = new javax.swing.JComboBox();
        cbx_modelo = new javax.swing.JComboBox();
        btn_cancelar = new javax.swing.JButton();
        btn_buscar = new javax.swing.JButton();
        lbl_marca = new javax.swing.JLabel();
        lbl_modelo = new javax.swing.JLabel();
        lbl_tipo_producto1 = new javax.swing.JLabel();
        cbx_tipoproducto = new javax.swing.JComboBox();

        lbl_tipo_producto.setBackground(new java.awt.Color(255, 255, 255));
        lbl_tipo_producto.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lbl_tipo_producto.setForeground(new java.awt.Color(0, 0, 102));
        lbl_tipo_producto.setText("Tipo de Producto");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SOVI");
        setIconImage(getIconImage());
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        lbl_titulo.setFont(new java.awt.Font("Cambria", 1, 18)); // NOI18N
        lbl_titulo.setForeground(new java.awt.Color(0, 0, 112));
        lbl_titulo.setText("BUSCAR PRODUCTO");

        cbx_marca.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbx_marcaItemStateChanged(evt);
            }
        });

        btn_cancelar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        btn_cancelar.setForeground(new java.awt.Color(0, 0, 102));
        btn_cancelar.setText("CANCELAR");
        btn_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelarActionPerformed(evt);
            }
        });

        btn_buscar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        btn_buscar.setForeground(new java.awt.Color(0, 0, 102));
        btn_buscar.setText("BUSCAR");
        btn_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_buscarActionPerformed(evt);
            }
        });

        lbl_marca.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lbl_marca.setForeground(new java.awt.Color(0, 0, 102));
        lbl_marca.setText("Marca");

        lbl_modelo.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lbl_modelo.setForeground(new java.awt.Color(0, 0, 102));
        lbl_modelo.setText("Modelo");

        lbl_tipo_producto1.setBackground(new java.awt.Color(255, 255, 255));
        lbl_tipo_producto1.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        lbl_tipo_producto1.setForeground(new java.awt.Color(0, 0, 102));
        lbl_tipo_producto1.setText("Tipo de Producto");

        cbx_tipoproducto.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbx_tipoproductoItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btn_buscar)
                .addGap(107, 107, 107)
                .addComponent(btn_cancelar)
                .addGap(183, 183, 183))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(cbx_tipoproducto, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cbx_marca, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cbx_modelo, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(lbl_titulo, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(36, 36, 36)
                            .addComponent(lbl_tipo_producto1)
                            .addGap(142, 142, 142)
                            .addComponent(lbl_marca)
                            .addGap(163, 163, 163)
                            .addComponent(lbl_modelo))))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(lbl_titulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbl_marca)
                    .addComponent(lbl_modelo)
                    .addComponent(lbl_tipo_producto1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbx_marca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbx_modelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbx_tipoproducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_cancelar)
                    .addComponent(btn_buscar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btn_cancelarActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
         cargar_combo_tipo_producto();                        
    }//GEN-LAST:event_formWindowOpened

    private void cbx_marcaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbx_marcaItemStateChanged
        cargar_combo_modelo(String.valueOf(cbx_marca.getSelectedItem()),String.valueOf(cbx_tipoproducto.getSelectedItem()));
    }//GEN-LAST:event_cbx_marcaItemStateChanged

    private void cbx_tipoproductoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbx_tipoproductoItemStateChanged
        cargar_combo_modelo(String.valueOf(cbx_marca.getSelectedItem()),String.valueOf(cbx_tipoproducto.getSelectedItem()));
    }//GEN-LAST:event_cbx_tipoproductoItemStateChanged

    private void btn_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_buscarActionPerformed
                
    }//GEN-LAST:event_btn_buscarActionPerformed

    public void cargar_combo_modelo(String desc_marca,String desc_tp)
    {                       
        obj_producto.getModelo().getMarca().setCodigo(0);
        obj_producto.getModelo().getMarca().setNombre_marca(desc_marca);        
        obj_ln.consultarcod(obj_producto.getModelo().getMarca());
                
        obj_producto.getTipo_prod().setCodigo(0);
        obj_producto.getTipo_prod().setDesc_tipo_producto(desc_tp);
        obj_ln.consultarcod(obj_producto.getTipo_prod());
                
        try{         
             cbx_modelo.setModel(obj_ln.consultar_combo(obj_producto));                      
         }
         catch(NullPointerException e)
         {
             LOG l=new LOG();
             JOptionPane.showMessageDialog(null,obj_ln.error_usuario(obj_producto.getError()),"ERROR",JOptionPane.ERROR_MESSAGE);
             String log="Tipo: "+obj_producto.getError().getTipo_error()+". Mensaje: "+obj_producto.getError().getDescripcion_error();
             l.escribir(log);                          
             this.dispose();            
         }         
    }
    
    public void cargar_combo_tipo_producto() {
        
         try{         
             cbx_tipoproducto.setModel(obj_ln.consultar_combo(obj_producto.getTipo_prod()));         
             cargar_combo_marca();
         }
         catch(NullPointerException e)
         {
             LOG l=new LOG();
             JOptionPane.showMessageDialog(null,obj_ln.error_usuario(obj_producto.getTipo_prod().getError()),"ERROR",JOptionPane.ERROR_MESSAGE);                    
             String log="Tipo: "+obj_producto.getTipo_prod().getError().getTipo_error()+". Mensaje: "+obj_producto.getTipo_prod().getError().getDescripcion_error();
             l.escribir(log);                          
             this.dispose();
             
         }                      
    }
    
    public void cargar_combo_marca() {        
         try
         {         
             cbx_marca.setModel(obj_ln.consultar_combo(obj_producto.getModelo().getMarca()));                  
             cargar_combo_modelo(String.valueOf(cbx_marca.getSelectedItem()),String.valueOf(cbx_tipoproducto.getSelectedItem()));
         }
         catch(NullPointerException e)
         {          
             LOG l=new LOG();
             JOptionPane.showMessageDialog(null,obj_ln.error_usuario(obj_producto.getModelo().getMarca().getError()),"ERROR",JOptionPane.ERROR_MESSAGE);                    
             String log="Tipo: "+obj_producto.getModelo().getMarca().getError().getTipo_error()+". Mensaje: "+obj_producto.getModelo().getMarca().getError().getDescripcion_error();
             l.escribir(log);
             this.dispose();
         }                          
    }
    
    public static void main(String args[]) {                
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IU_BUSCAR_PRODUCTO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IU_BUSCAR_PRODUCTO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IU_BUSCAR_PRODUCTO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IU_BUSCAR_PRODUCTO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IU_BUSCAR_PRODUCTO().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_buscar;
    private javax.swing.JButton btn_cancelar;
    private javax.swing.JComboBox cbx_marca;
    private javax.swing.JComboBox cbx_modelo;
    private javax.swing.JComboBox cbx_tipoproducto;
    private javax.swing.JLabel lbl_marca;
    private javax.swing.JLabel lbl_modelo;
    private javax.swing.JLabel lbl_tipo_producto;
    private javax.swing.JLabel lbl_tipo_producto1;
    private javax.swing.JLabel lbl_titulo;
    // End of variables declaration//GEN-END:variables
}
